//
//  SignManager.swift
//  SeeFM_Swift
//
//  Created by CBCT_MBP on 2021/6/23.
//
//  签名助手

let kAppKey: String = "O2018@(*ZYC$1024$662*)@8102O"

import UIKit

class SignManager: NSObject {
    
    class func getRegisterSign() -> String {
        
        let token = "TOKEN"
        let salt  = "SALT"
        let tmp   = Self.getTimeStamp()
        var sign  = NSString.md5HexDigest(String.init(format: "%@%@%@", token,tmp,salt))
        let index = Int(arc4random() % 6)
        sign      = (sign as NSString).substring(from: index)
        return sign
    }
    
    class func getApiSign() -> String {
        let token = Self.getToken()
        let tmp   = Self.getTimeStamp()
        let salt  = Self.getSalt()
        var sign  = NSString.md5HexDigest(String.init(format: "%@%@%@", token,tmp,salt))
        let index = Int(arc4random() % 6)
        sign      = (sign as NSString).substring(from: index)
        
        return sign
    }
    
    class func getToken() -> String {
        let registerModel = RegisterManager.shared.getRegisterInfo()
        guard let u = AppUser.userInfo() else { return registerModel?.token ?? "TOKEN" }
        return u.token ?? "TOKEN"
    }
    
    class func getDeviceToken() -> String {
        guard let m = RegisterManager.shared.getRegisterInfo() else { return "DEVICE_TOKEN" }
        return m.token ?? "TOKEN"
    }
    
    class func getSalt() -> String {
        let m = RegisterManager.shared.getRegisterInfo()
        let u = AppUser.userInfo()
        return Login.isLogin() ? u?.salt ?? "" : m?.salt ?? ""
    }
    
    class func getTimeStamp() -> String {
        let t = Self.getSignTimeString() + kAppKey
        let md5t = NSString.md5HexDigest(t)
        let ts = (md5t as NSString).substring(to: md5t.count - 3)
        return ts
    }

    class func getSignTimeString() -> String {
        let date = NSDate.init(timeIntervalSinceNow: 0)
        let a = date.timeIntervalSince1970
        var timeStr = String.init(format: "%.0f", a)
        
        if ConfigGlobalInfo.shared.timeDiff != 0 {
            let minusValue = Int(a) * 1000 + ConfigGlobalInfo.shared.timeDiff!
            timeStr = String.init(format: "%ld", minusValue)
        }else {
            let minusValue = Int(timeStr)! * 1000
            timeStr = String.init(format: "%ld", minusValue)
        }
        return timeStr
    }
}

struct RegisterModel: HandyJSON {
    
    var token: String?
    var rc_token:String?
    var salt: String?
    var group_id: String?

}


struct LocationModel: HandyJSON {
    
    var longitude: String?
    var latitude: String?
    
}


